using System;
using System.Collections.Generic;
using System.Text;
using AugContracts;
using AugDB;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Data.DLinq;
using System.Query;



namespace AugService {

  public class MembershipService: IMembershipService {
  
    public string RegisterMember(MembershipData aData) {

      MembershipUser xMember = Membership.CreateUser(aData.Username, aData.Password, aData.Email);
      if (xMember != null) {
        //return xMember.ProviderUserKey.ToString();
        return Global._RM.GetString("RSUserIdCannotBeNull");
      } else {
        return null;
      }

    }
    
    public void UpdateUserData(UserData aData) {
    
     AugData xDB = new AugData(Global._ConnectionString);
     
     var xUser = xDB.UserDetails.Single(u => u.UserId == aData.UserId);
     
     if (xUser != null) {
       
      xUser.NameFirst = aData.NameFirst;
      xUser.NameLast = aData.NameLast;
      xUser.Address = aData.Address;
      xUser.Bio = aData.Bio;
      xUser.CellPhone = aData.CellPhone;
      xUser.City = aData.City;
      xUser.Country.CountryId = (short)aData.CountryId; 
      xUser.IsPublic = aData.IsPublic;
      xUser.MSNIdent = aData.MSNIdent;
      xUser.SecondaryEmail = aData.SecondaryEmail;
      xUser.SkypeIdent = aData.SkypeIdent;
      xUser.State = aData.State;
      xUser.Telephone = aData.Telephone;
      xUser.YahooIdent = aData.YahooIdent;
      
      xDB.SubmitChanges();
     } else {
      // New user...if 
      
      if (aData.UserId != null) {
      
        UserDetail xNewUser = new UserDetail();
        
        xNewUser.NameFirst = aData.NameFirst;
        xNewUser.NameLast = aData.NameLast;
        xNewUser.Address = aData.Address;
        xNewUser.Bio = aData.Bio;
        xNewUser.CellPhone = aData.CellPhone;
        xNewUser.City = aData.City;
        xNewUser.Country.CountryId = (short)aData.CountryId; 
        xNewUser.IsPublic = aData.IsPublic;
        xNewUser.MSNIdent = aData.MSNIdent;
        xNewUser.SecondaryEmail = aData.SecondaryEmail;
        xNewUser.SkypeIdent = aData.SkypeIdent;
        xNewUser.State = aData.State;
        xNewUser.Telephone = aData.Telephone;
        xNewUser.YahooIdent = aData.YahooIdent;
        xNewUser.UserId = aData.UserId;
        
        xDB.UserDetails.Add(xNewUser);
        xDB.SubmitChanges();
     } else {
      new Exception(Global._RM.GetString("RSUserIdCannotBeNull"));
      }     
    }  
    
    }
    
    public UserData GetUserData(string aUserId) {
    
     AugData xDB = new AugData(Global._ConnectionString);
     Guid xGuid = new Guid(aUserId);
     var xUser = xDB.UserDetails.Single(u => u.UserId == xGuid);
     
     if (xUser != null) {
       UserData xResult = new UserData();
       
       xResult.NameFirst = xUser.NameFirst;
       xResult.NameLast = xUser.NameLast;
       xResult.Address = xUser.Address;
       xResult.Bio = xUser.Bio;
       xResult.CellPhone = xUser.CellPhone;
       xResult.City = xUser.City;
       xResult.CountryId = xUser.Country.CountryId;
       xResult.IsPublic = xUser.IsPublic;
       xResult.MSNIdent = xUser.MSNIdent;
       xResult.SecondaryEmail = xUser.SecondaryEmail;
       xResult.SkypeIdent = xUser.SkypeIdent;
       xResult.State = xUser.State;
       xResult.Telephone = xUser.Telephone;
       xResult.YahooIdent = xUser.YahooIdent;
       xResult.UserId = xUser.UserId;
       return xResult;
     } else {
      return null;
     }   
     
    
    }
    
  }
}
